package com.yuexun.dhdoctor.module.commun.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.base.BaseKJActivity;
import com.yuexun.dhdoctor.baselistener.OnDataBackListener;
import com.yuexun.dhdoctor.module.Contract.adapter.CreateGroupAdapter;
import com.yuexun.dhdoctor.module.Contract.bean.ChildDepartmentsBean;
import com.yuexun.dhdoctor.module.Contract.bean.DepartOrPeopleData;
import com.yuexun.dhdoctor.module.Contract.bean.GroupGroupData;
import com.yuexun.dhdoctor.module.Contract.bean.People;
import com.yuexun.dhdoctor.module.commun.ui.fragment.Frag_DepartAndGroup_Depart;
import com.yuexun.dhdoctor.module.commun.ui.fragment.Frag_DepartAndGroup_Group;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/3/27 0027.
 * <p>
 * 回调10004
 */

public class Act_DepartAndGroup extends BaseKJActivity implements OnDataBackListener {

    @Bind(R.id.depTitle)
    Button depTitle;
    @Bind(R.id.contactTitle)
    Button contactTitle;
    @Bind(R.id.grid)
    GridView grid;
    @Bind(R.id.addmember_sure_btn)
    TextView addmemberSureBtn;

    public ArrayList departmentBeans = new ArrayList<>();

    private ArrayList<Fragment> fragmentsList = new ArrayList<>();
    private Frag_DepartAndGroup_Depart departmentActivity;
    private Frag_DepartAndGroup_Group groupActivity;
    private CreateGroupAdapter adapter;

    //0：不能选择自己  1：能选择
    private int isSelectMe;

    @Override
    public void setRootView() {
        super.setRootView();
        setContentView(R.layout.act_departandgroup);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        departmentBeans = (ArrayList) getIntent().getExtras().getSerializable("departmentBeans");
        isSelectMe = getIntent().getExtras().getInt("isSelectMe", 0);

        fragmentsList.clear();
        departmentActivity = new Frag_DepartAndGroup_Depart();
        groupActivity = new Frag_DepartAndGroup_Group();
        groupActivity.setDepartment(departmentBeans, this, isSelectMe);
        departmentActivity.setDepartment(departmentBeans, this, isSelectMe);

        depTitle.setSelected(true);
        changeFragment(R.id.group_content, departmentActivity);
        setGridView();
    }

    @Override
    @OnClick({R.id.back, R.id.depTitle, R.id.contactTitle, R.id.addmember_sure_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.depTitle:
                changeFragment(R.id.group_content, departmentActivity);
                depTitle.setSelected(true);
                contactTitle.setSelected(false);
                break;
            case R.id.contactTitle:
                changeFragment(R.id.group_content, groupActivity);
                depTitle.setSelected(false);
                contactTitle.setSelected(true);
                break;
            case R.id.addmember_sure_btn:
                Bundle bundle = new Bundle();
                Intent intent = new Intent();
                bundle.putSerializable("departmentBeans", departmentBeans);
                intent.putExtras(bundle);
                setResult(10004, intent);
                finish();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case 10003:
                departmentBeans = (ArrayList<People>) data.getExtras().getSerializable("departmentBeans");
                finishView(departmentBeans);
                break;
            case 0x001:
                departmentBeans = ((ArrayList<People>) data.getExtras().getSerializable("departmentBeans"));
                setGridView();
                break;
        }
    }

    @Override
    public void showData(Bundle bundle) {
        if (bundle != null) {
            departmentBeans = (ArrayList<People>) bundle.getSerializable("departmentBeans");
            setGridView();
        }
    }

    private void setGridView() {
        int size = departmentBeans.size();

        List<DepartOrPeopleData> datas = new ArrayList<>();

        for (int i = 0; i < departmentBeans.size(); i++) {
            if (departmentBeans.get(i) instanceof ChildDepartmentsBean) {
                DepartOrPeopleData data = new DepartOrPeopleData();
                data.setDataId(((ChildDepartmentsBean) departmentBeans.get(i)).getDepartment_id() + "");
                data.setDataName(((ChildDepartmentsBean) departmentBeans.get(i)).getDepartment_name());
                datas.add(data);
            } else if (departmentBeans.get(i) instanceof GroupGroupData) {
                DepartOrPeopleData data = new DepartOrPeopleData();
                data.setDataId(((GroupGroupData) departmentBeans.get(i)).getGroupId() + "");
                data.setDataName(((GroupGroupData) departmentBeans.get(i)).getTitle());
                datas.add(data);
            }
        }

        if (size == 0) {
            addmemberSureBtn.setBackgroundResource(R.color.doc_advice_02);
            addmemberSureBtn.setEnabled(false);
        } else {
            addmemberSureBtn.setBackgroundResource(R.color.doc_advice_01);
            addmemberSureBtn.setEnabled(true);
        }

        addmemberSureBtn.setText("确认");

        int length = 80;

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        float density = dm.density;
        int gridviewWidth = (int) (size * (length + 4) * density);
        int itemWidth = (int) (length * density);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                gridviewWidth, LinearLayout.LayoutParams.FILL_PARENT);
        grid.setLayoutParams(params); // 重点
        grid.setColumnWidth(itemWidth); // 重点
        grid.setHorizontalSpacing(5); // 间距
        grid.setStretchMode(GridView.NO_STRETCH);
        grid.setNumColumns(size); // 重点

        adapter = new CreateGroupAdapter(grid, datas, R.layout.item_send_addmember);
        grid.setAdapter(adapter);
    }

    private void finishView(ArrayList departmentBeans) {
        setGridView();
        if (departmentBeans.size() == 0) {
            Toast.makeText(this, "请选择接收对象", Toast.LENGTH_SHORT).show();
        } else {
            Bundle bundle = new Bundle();
            Intent intent = new Intent();
            bundle.putSerializable("departmentBeans", departmentBeans);
            intent.putExtras(bundle);
            setResult(10004, intent);
            finish();
        }
    }
}
